package com.situ.crm.controller;

import com.situ.crm.util.Result;
import com.situ.crm.pojo.entity.Role;
import com.situ.crm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;
import java.util.Date;

@ApiIgnore
@RequestMapping("/role")
@Controller
public class RoleController {
	@Autowired
	private IRoleService roleService;

	@RequestMapping("/getRolePage")
	public String getRolePage() {
		return "/role/role_list";
	}

	@RequestMapping("/selectByPage")
	@ResponseBody
	public Result selectByPage(Integer page, Integer limit, Role role, Date time) {
		System.out.println(time);
		Result result = roleService.selectByPage(page, limit, role);
		return result;
	}

	@RequestMapping("/deleteById")
	@ResponseBody
	public Result deleteById(Integer id) {
		boolean isSuccess = roleService.deleteById(id);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return  Result.error("删除失败");
		}
	}

	@RequestMapping("/deleteAll")
	@ResponseBody
	public Result deleteAll(String ids) {
		boolean isSuccess = roleService.deleteAll(ids);
		if (isSuccess) {
			return Result.success("删除成功");
		} else {
			return  Result.error("删除失败");
		}
	}

	@RequestMapping("/getAddPage")
	public String getAddPage() {
		return "/role/role_add";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Result add(Role role, String permissions) {
		System.out.println(role);
		System.out.println(permissions);
		boolean isSuccess = roleService.add(role, permissions);
		if (isSuccess) {
			return Result.success("添加成功");
		} else {
			return  Result.error("添加失败");
		}
	}
	
	@RequestMapping("/update")
	@ResponseBody
	public Result update(Role role, Integer[] permissions) {
		// permissions: 7,9,10,8,4,5,6
		System.out.println(role);
		System.out.println(Arrays.toString(permissions));
		boolean isSuccess = roleService.update(role, permissions);
		if (isSuccess) {
			return Result.success("更新成功");
		} else {
			return  Result.error("更新失败");
		}
	}
	
	@RequestMapping("/getUpdatePage")
	public String getUpdatePage() {
		return "/role/role_update";
	}
	
	@RequestMapping("/selectRoleAndPermisssions")
	@ResponseBody
	public Result selectRoleAndPermisssions(Integer roleId) {
		return roleService.selectRoleAndPermisssions(roleId);
	}
	
	@RequestMapping("/selectAllRoles")
	@ResponseBody
	public Result selectAllRoles() {
		return roleService.selectAllRoles();
	}
}
